﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StoreVnCore.DataAccessObject;
using StoreVnCore.Entity;
using StoreVnCore.ExceptionManager;
using System.Data;

namespace StoreVnCore.Business
{
    /// <summary>
    /// Business Layer of TypeOfContentBO.
    /// </summary>
    public class TypeOfContentBO
    {
        #region Basic Functions
        public TypeOfContentEntity AddNew(TypeOfContentEntity typeOfContent)
        {
            try
            {
                TypeOfContentDAO typeOfContentDAO = new TypeOfContentDAO();
                return (TypeOfContentEntity)typeOfContentDAO.Insert(typeOfContent);
            }
            catch (DataAccessException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new BusinessException("TypeOfContentBO::AddNew::Error", ex);
            }
        }

        public TypeOfContentEntity Save(TypeOfContentEntity typeOfContent)
        {
            try
            {
                TypeOfContentDAO typeOfContentDAO = new TypeOfContentDAO();
                return (TypeOfContentEntity)typeOfContentDAO.Update(typeOfContent);
            }
            catch (DataAccessException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new BusinessException("TypeOfContentBO::Save::Error", ex);
            }
        }

        public void Delete(int id)
        {
            try
            {
                TypeOfContentDAO typeOfContentDAO = new TypeOfContentDAO();
                typeOfContentDAO.Delete(id);
            }
            catch (DataAccessException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new BusinessException("TypeOfContentBO::Delete::Error", ex);
            }
        }

        public TypeOfContentEntity SelectOne(int id)
        {
            try
            {
                TypeOfContentDAO typeOfContentDAO = new TypeOfContentDAO();
                return (TypeOfContentEntity)typeOfContentDAO.SelectOne(id);
            }
            catch (DataAccessException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new BusinessException("TypeOfContentBO::SelectOne::Error", ex);
            }
        }

        public List<TypeOfContentEntity> SelectList()
        {
            try
            {
                TypeOfContentDAO typeOfContentDAO = new TypeOfContentDAO();
                DataTable dt = typeOfContentDAO.SelectAll();
                if (dt != null && dt.Rows.Count > 0)
                {
                    List<TypeOfContentEntity> listTypeOfContent = new List<TypeOfContentEntity>();
                    foreach (DataRow dr in dt.Rows)
                    {
                        TypeOfContentEntity typeOfContent = new TypeOfContentEntity();
                        //Id
                        if (dr[FieldNames.TypeOfContent.Id] != null && dr[FieldNames.TypeOfContent.Id].ToString() != "")
                            typeOfContent.Id = (int)dr[FieldNames.TypeOfContent.Id];

                        //Name
                        if (dr[FieldNames.TypeOfContent.Name] != null && dr[FieldNames.TypeOfContent.Name].ToString() != "")
                            typeOfContent.Name = (string)dr[FieldNames.TypeOfContent.Name];

                        //Description
                        if (dr[FieldNames.TypeOfContent.Description] != null && dr[FieldNames.TypeOfContent.Description].ToString() != "")
                            typeOfContent.Description = (string)dr[FieldNames.TypeOfContent.Description];

                        listTypeOfContent.Add(typeOfContent);
                    }
                    return listTypeOfContent;
                }
                return null;
            }
            catch (DataAccessException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new BusinessException("TypeOfContentBO::SelectList::Error", ex);
            }
        }

        public DataTable SelectAll()
        {
            try
            {
                TypeOfContentDAO typeOfContentDAO = new TypeOfContentDAO();
                return typeOfContentDAO.SelectAll();
            }
            catch (DataAccessException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new BusinessException("TypeOfContentBO::SelectAll::Error", ex);
            }
        }

        #endregion
    }
}
